package com.atguigu.jdbc;

import java.sql.*;
import java.util.Scanner;

public class AddUser {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入用户名:");
        String user = input.next();
        System.out.print("请输入密码:");
        String password = input.next();
        System.out.print("请输入邮箱:");
        String email = input.next();
        //1.加载类驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接数据库
        String url = "jdbc:mysql://localhost:3306/day04_test01_bookstore";
        Connection conn = DriverManager.getConnection(url, "root", "123456");

        //3.操作数据库
        String sql = "insert into users values(null ,?,PASSWORD(?),?)";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,user);
        pst.setString(2,password);
        pst.setString(3,email);

        int len = pst.executeUpdate();
        System.out.println(len>0?"添加成功":"添加失败");

        pst.close();
        conn.close();
        input.close();
    }
}
